"""
@Description :   
@Author      :   Darcy 
@Time        :   2024/12/29 14:15:17
"""

import numpy as np
from precision_data import fp
from collocated_sharing import *
from solver import *

def collocated_segregated(it, case, fluid, fluidboundary, post):
    
    # Store old solution
    case.T0 = np.copy(case.T)

    dim     = case.dim
    
    ncx     = case.ncx    
    ncy     = case.ncy
    ncz     = case.ncz
        
    ncoef   = case.ncoef
    dt      = case.dt
    
    x       = case.x 
    y       = case.y  
    z       = case.z 
    
    T       = case.T
    T0      = case.T0   
    uf      = case.uf     
    vf      = case.vf     
    wf      = case.wf     
    cT      = case.cT     
    
    niter_T = case.niter_T
    relax_T = case.relax_T
    
    ieqn = case.ieqn                        # 方程类型
    eqn_conduction = case.eqn_conduction    # 在structured_mesh.py文件中方程的类型是温度场
    temp_tol = case.temp_tol                # 非线性迭代的收敛误差判定值
    res_T = case.res_T
    stop_sim = case.stop_sim
    
    # 定义与流体对象相关的局部变量
    spht     = fluid.spht
    con      = fluid.con 
    dens     = fluid.dens
    heat_src = fluid.heat_src
    
    if ieqn == eqn_conduction:  # 如果方程类型是温度场类型就继续执行以下操作
        
        # 在这个分离求解器中，最重要的核心是在下面这三行代码中，先计算温度方程coefs，然后对网格的边界类型做处理，修改coefs，最后进行雅可比迭代或高斯赛戴尔迭代
        # 雅可比迭代或高斯赛戴尔迭代是在当前计算好的温度方程coefs下迭代求解的，但是这个温度方程coefs又与温度场有关，故需要在雅可比迭代或高斯赛戴尔迭代
        # 收敛后继续以新的温度场去求解温度方程coefs，并且在这个新的温度方程coefs下继续迭代求解新的温度场，
        # 最终在温度方程coefs满足一定精度的情况下结束整体的迭代，这个整体的迭代就是在主文件中的那个循环迭代。
        # 注意，在这个案例中温度方程的coefs不变，但是在之后的有些例子中会发生变化
        
        # 计算温度方程coefs
        conduction_coefs(case, dim, ncx, ncy, ncz, ncoef, dt, spht, con, heat_src, x, y, z, dens, T, uf, vf, wf, cT)
        # 对网格的边界类型做处理，修改coefs
        conduction_coef_bcs(case, fluidboundary, dim, ncx, ncy, ncz, ncoef, dt, con, x, y, z, dens, T, cT)
        
        initzero = False
        # 进行雅克比迭代
        scalar_pj(case, post, dim, it, niter_T, relax_T, ncx, ncy, ncz, ncoef, cT, T, initzero, res_T) 
        # 进行高斯赛德尔迭代
        # scalar_gs(case, post, dim, it, niter_T, relax_T, ncx, ncy, ncz, ncoef, cT, t, initzero, res_T)
        
        # 这个结果与非线性迭代的收敛误差判定值对比
        (case.l2_T, case.l2_max_T) = eqn_scalar_norm2(case, dim, it, ncx, ncy, ncz, T0, T, 'temp')

        if case.l2_T/case.l2_max_T < temp_tol:      #如果满足条件就令stop_sim为真，程序将在主文件那里跳出迭代循环
            stop_sim = True
            print('')
            print('----------------------------')
            print('Final iter =', it)
            print('it, l2_T/l2_max_T', it, case.l2_T/case.l2_max_T)
            print('----------------------------')
        
        return stop_sim